package net.linxuehua.server.controller.admapi;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.linxuehua.commons.core.R;
import net.linxuehua.server.dto.CookAddDTO;
import net.linxuehua.server.dto.CookUpdateDTO;
import net.linxuehua.server.po.Cook;
import net.linxuehua.server.po.Food;
import net.linxuehua.server.service.CookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author chenyan
 * @createTime 2021/1/12 9:41
 * @description description
 */

@Slf4j
@Api(tags = {"做菜接口"})
@RestController
@RequestMapping(value = "/admapi/cook")
public class CookController {

    @Autowired
    private CookService cookService;

    @ApiOperation(value = "获取订单信息")
    @GetMapping(value = "/{userId}")
    public R getInfo(@PathVariable Integer userId) {
        QueryWrapper<Cook> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<Cook> cookList = cookService.list(queryWrapper);
        return R.data(cookList);
    }


    @ApiOperation(value = "新增订单")
    @PostMapping("/add")
    public R add(@RequestBody CookAddDTO cookAddDTO) {
        if (cookAddDTO.getUserId() == null || cookAddDTO.getUserId() == 0) {
            return R.error("用户id不能为空");
        }
        Cook cook = new Cook();
        cook.setUserId(cookAddDTO.getUserId());
        cook.setDeskId(cookAddDTO.getDeskId());
        cook.setTime(cookAddDTO.getTime());
        List<Food> foodList = cookAddDTO.getFoodList();
        int[] foodId = new int[foodList.size()];
        int[] foodNum = new int[foodList.size()];
        int price = 0;
        int i = 0;
        for (Food food : foodList) {
            foodId[i] = food.getFoodId();
            foodNum[i] = food.getFoodNum();
            price += food.getPrice() * food.getFoodNum();
            i++;
        }
        cook.setFoodId(cookService.toString(foodId));
        cook.setFoodNum(cookService.toString(foodNum));
        cook.setPrice(price);
        boolean temp = cookService.save(cook);
        return temp ? R.ok("添加成功") : R.error("添加失败");
    }


    @SneakyThrows
    @ApiOperation(value = "删除订单")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        Cook cook = cookService.getById(id);
        boolean result = cookService.isOutTime(cook.getCreateTime());
        if (result == false) {
            return R.error("订单超过5min无法删除");
        }
        boolean temp = cookService.removeById(id);
        return temp ? R.ok("删除成功") : R.error("删除失败");
    }

    //修改
    @ApiOperation(value = "修改订单")
    @PutMapping(value = "/update")
    public R update(@RequestBody CookUpdateDTO cookUpdateDTO) {
        if (cookUpdateDTO.getUserId() == null && cookUpdateDTO.getUserId() == null) {
            return R.error("修改失败，id不能为空");
        }
        QueryWrapper<Cook> cookQueryWrapper = new QueryWrapper<>();
        cookQueryWrapper.eq("cook_id", cookUpdateDTO.getCookId());
        Cook oldCook = cookService.getOne(cookQueryWrapper);
        if (oldCook == null) {
            return R.error("订单不存在");
        }
        boolean result = cookService.isOutTime(oldCook.getCreateTime());
        if (result == false) {
            return R.error("订单超过5min无法更改");
        }
        oldCook.setTime(cookUpdateDTO.getTime());
        oldCook.setDeskId(cookUpdateDTO.getDeskId());
        List<Food> foodList = cookUpdateDTO.getFoodList();
        int[] foodId = new int[foodList.size()];
        int[] foodNum = new int[foodList.size()];
        int price = 0;
        int i = 0;
        for (Food food : foodList) {
            foodId[i] = food.getFoodId();
            foodNum[i] = food.getFoodNum();
            price += food.getPrice() * food.getFoodNum();
            i++;
        }
        oldCook.setFoodId(cookService.toString(foodId));
        oldCook.setFoodNum(cookService.toString(foodNum));
        oldCook.setPrice(price);
        boolean temp = cookService.updateById(oldCook);
        return temp ? R.ok("修改成功") : R.error("修改失败");
    }

}
